<?php
namespace lib;
class LocalRedis {
    private $config = null;
    private $client = null;
    private static $instance = null;
    public function __construct() {
        $this->client = new \Swoole\Coroutine\Redis();
        $this->client->connect('127.0.0.1', 6379);
    }
    
    /* public static function getRedis() {
        if(self::$instance === null) {
            self::$instance = new  self();
        }
        return self::$instance;
    } */
    
    public function set($key, $value) {
        if(is_array($value)){
            $value = json_encode($value);
        }
        return $this->client->set($key, $value);
    }
    
    public function setex($key, $value, $expire = 0) {
        if(is_array($value)){
            $value = json_encode($value);
        }
        
        if($expire <= 0){
            return false;
        }
        
        return $this->client->setex($key, $expire, $value);
    }
    
    public function lpush($key, $value) {
        if(is_array($value)){
            $value = json_encode($value);
        }
        $this->client->lpush($key, $value);
    }
    
    public function rpop($key) {
        return $this->client->rpop($key);
    }
    
    public function get($key) {
        $result = '';
        try {
            $result = $this->client->get($key);
        } catch (\Exception $e) {
        }
        
        return $result;
    }
    
    public function del($key) {
        $this->client->del($key);
    }
    
    public function close() {
        $this->client->close();
        $this->client = null;
    }
}